# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： auto_operate_page.py
# @Time：2023/8/1 17:40
# @Author：lizhixiong
import allure

from common.commin.commin import object_left_click, object_send_keys, action_delete
from common.log.log import log
from data.names import AutoMarketing, PublicSel
from data.randomly_generated_data import generate_random_intro, generate_random_product_name
from po.jinhua.ht.jinhua_ht_main.jinhua_base import JHBase
from po.jinhua.ht.jinhua_ht_main.jinhua_ht_main_page import JHHTHomePage


class AutoOperatePage(JHBase):
    """
    自动化运营菜单下所有页面
    """

    @allure.step('新增用户群')
    def add_users(self, level='普通会员'):
        """新增用户群"""
        try:
            log.info('*****新增用户群*****')
            name = generate_random_product_name()[-8:]
            object_left_click(self.driver, AutoMarketing.el_users_1())
            object_send_keys(self.driver, AutoMarketing.el_users_2(), name)
            object_left_click(self.driver, AutoMarketing.el_users_3())
            object_left_click(self.driver, AutoMarketing.el_users_4())
            if level == '普通会员':
                object_left_click(self.driver, AutoMarketing.el_users_5())
            elif level == '精英会员':
                object_left_click(self.driver, AutoMarketing.el_users_6())
            elif level == 'VIP会员':
                object_left_click(self.driver, AutoMarketing.el_users_7())
            elif level == '合作伙伴':
                object_left_click(self.driver, AutoMarketing.el_users_8())
            # 补充花王云创
            elif level == 'VIP':
                object_left_click(self.driver, AutoMarketing.el_users_6_1())
            elif level == 'VVIP':
                object_left_click(self.driver, AutoMarketing.el_users_7_1())
            elif level == 'SVIP':
                object_left_click(self.driver, AutoMarketing.el_users_8_1())
            object_left_click(self.driver, AutoMarketing.el_users_9())
            object_send_keys(self.driver, AutoMarketing.el_users_9(), generate_random_intro())
            object_left_click(self.driver, AutoMarketing.el_users_10())
            log.info(f'新增用户群成功：{name}')
            return name
        except Exception as e:
            log.error(f'新增用户群出现错误，异常为{e}')
            raise e

    @allure.step('搜索用户群')
    def search_users(self, keyword):
        """搜索用户群"""
        try:
            log.info('*****搜索用户群*****')
            JHHTHomePage(self.driver).open_menu('yg', '用户群管理')
            object_send_keys(self.driver, AutoMarketing.el_users_22(), keyword)
            object_left_click(self.driver, PublicSel.el_icon_search_2())
        except Exception as e:
            log.error(f'搜索用户群出现错误，异常为{e}')
            raise e

    @allure.step('删除用户群')
    def delete_users(self, keyword):
        """搜索用户群"""
        try:
            log.info('*****删除用户群*****')
            self.search_users(keyword)
            action_delete(self.driver)
            log.info('删除用户群成功~')
        except Exception as e:
            log.error(f'删除用户群出现错误，异常为{e}')
            raise e
